<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>generate (Generator)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File generate.rb, line 66</span>
  <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">generate</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
    <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
      <span class="ruby-ivar">@params</span> = <span class="ruby-constant">ARGV</span>
    <span class="ruby-keyword kw">else</span>
      <span class="ruby-ivar">@params</span> = <span class="ruby-identifier">args</span>
    <span class="ruby-keyword kw">end</span>

    <span class="ruby-comment cmt"># command line parsing</span>
    <span class="ruby-identifier">opts</span> = <span class="ruby-constant">OptionParser</span>.<span class="ruby-identifier">new</span>
    <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">on</span>(<span class="ruby-value str">&quot;-h&quot;</span>, <span class="ruby-value str">&quot;--help&quot;</span>) { <span class="ruby-constant">RDoc</span><span class="ruby-operator">::</span><span class="ruby-identifier">usage</span> }
    <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">on</span>(<span class="ruby-value str">&quot;-f FUNCTION&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">fun</span><span class="ruby-operator">|</span> <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;functions is #{fun}&quot;</span>}
    <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-ivar">@params</span>) <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">RDoc</span><span class="ruby-operator">::</span><span class="ruby-identifier">usage</span>
    
    <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@params</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-operator">||</span> <span class="ruby-ivar">@params</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;help&quot;</span>
      <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@embedded</span>
        <span class="ruby-keyword kw">return</span> <span class="ruby-node">&quot;#{help}&quot;</span>
      <span class="ruby-keyword kw">else</span>
        <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{help}&quot;</span>
        <span class="ruby-keyword kw">return</span>
      <span class="ruby-keyword kw">end</span>
    <span class="ruby-keyword kw">end</span>

    <span class="ruby-identifier">opt_fun</span> = <span class="ruby-identifier">getopt</span>(<span class="ruby-value str">&quot;f&quot;</span>)
    <span class="ruby-identifier">opt_num</span> = <span class="ruby-identifier">getopt</span>(<span class="ruby-value str">&quot;n&quot;</span>).<span class="ruby-identifier">nil?</span> <span class="ruby-value">? </span><span class="ruby-value">100</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">getopt</span>(<span class="ruby-value str">&quot;n&quot;</span>).<span class="ruby-identifier">to_i</span>
    <span class="ruby-identifier">opt_sigma</span> = <span class="ruby-identifier">getopt</span>(<span class="ruby-value str">&quot;s&quot;</span>).<span class="ruby-identifier">nil?</span> <span class="ruby-value">? </span><span class="ruby-value">0</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">getopt</span>(<span class="ruby-value str">&quot;s&quot;</span>).<span class="ruby-identifier">to_f</span>
    <span class="ruby-identifier">opt_psigma</span> = <span class="ruby-identifier">getopt</span>(<span class="ruby-value str">&quot;ps&quot;</span>).<span class="ruby-identifier">nil?</span> <span class="ruby-value">? </span><span class="ruby-value">0</span><span class="ruby-value">.1</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">getopt</span>(<span class="ruby-value str">&quot;ps&quot;</span>).<span class="ruby-identifier">to_f</span>
    <span class="ruby-identifier">range_lower</span> , <span class="ruby-identifier">range_upper</span> = <span class="ruby-identifier">getopt</span>(<span class="ruby-value str">&quot;r&quot;</span>).<span class="ruby-identifier">split</span>(<span class="ruby-value str">&quot;:&quot;</span>).<span class="ruby-identifier">collect</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">==</span> <span class="ruby-value str">''</span> <span class="ruby-operator">?</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">i</span>.<span class="ruby-identifier">to_i</span> } <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">getopt</span>(<span class="ruby-value str">&quot;r&quot;</span>).<span class="ruby-identifier">nil?</span>     

    <span class="ruby-comment cmt"># do the job</span>

    <span class="ruby-identifier">f</span> = <span class="ruby-constant">Functions</span>.<span class="ruby-identifier">new</span>
    <span class="ruby-identifier">generator</span> = <span class="ruby-identifier">eval</span>(<span class="ruby-value str">&quot;f.send(opt_fun)&quot;</span>)

    <span class="ruby-identifier">y_values</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>
    (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-identifier">opt_num</span>).<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">y</span> = <span class="ruby-identifier">generator</span>.<span class="ruby-identifier">call</span>(<span class="ruby-identifier">x</span>)
      <span class="ruby-identifier">mu</span> = <span class="ruby-value">0</span>
      <span class="ruby-identifier">sigma</span> = <span class="ruby-identifier">opt_sigma</span>.<span class="ruby-identifier">zero?</span> <span class="ruby-value">? </span><span class="ruby-identifier">y</span><span class="ruby-operator">*</span><span class="ruby-identifier">opt_psigma</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">opt_sigma</span>
      <span class="ruby-identifier">z_delta</span> = <span class="ruby-constant">Statistics2</span>.<span class="ruby-identifier">pnormaldist</span>(<span class="ruby-identifier">rand</span>) <span class="ruby-comment cmt"># generate a gaussian noise</span>
      <span class="ruby-identifier">y_delta</span> = <span class="ruby-identifier">z_delta</span> <span class="ruby-operator">*</span> <span class="ruby-identifier">sigma</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">mu</span>
      <span class="ruby-identifier">y_values</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">y</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">y_delta</span>
    <span class="ruby-keyword kw">end</span>

    <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">range_lower</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword kw">or</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">range_upper</span>.<span class="ruby-identifier">nil?</span> 
      <span class="ruby-comment cmt"># locate y_max and y_min</span>
      <span class="ruby-identifier">y_max</span> , <span class="ruby-identifier">y_min</span> = <span class="ruby-value">-10</span><span class="ruby-operator">**</span><span class="ruby-value">100</span> , <span class="ruby-value">10</span><span class="ruby-operator">*</span><span class="ruby-value">100</span>
      <span class="ruby-identifier">y_values</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span> 
        <span class="ruby-identifier">y_max</span> = <span class="ruby-identifier">y_max</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">y</span> <span class="ruby-value">? </span><span class="ruby-identifier">y_max</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">y</span>
        <span class="ruby-identifier">y_min</span> = <span class="ruby-identifier">y_min</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">y</span> <span class="ruby-value">? </span><span class="ruby-identifier">y_min</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">y</span>
      <span class="ruby-keyword kw">end</span>

      <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">range_lower</span>.<span class="ruby-identifier">nil?</span> 
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">range_upper</span>.<span class="ruby-identifier">nil?</span> 
          <span class="ruby-comment cmt"># translate lower bound</span>
          <span class="ruby-identifier">y_values</span>.<span class="ruby-identifier">collect!</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span> <span class="ruby-identifier">y</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">y_min</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">range_lower</span>  }
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">end</span>

      <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">not</span> <span class="ruby-identifier">range_upper</span>.<span class="ruby-identifier">nil?</span>
        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">range_lower</span>.<span class="ruby-identifier">nil?</span>
          <span class="ruby-comment cmt"># translate upper bound</span>
          <span class="ruby-identifier">y_values</span>.<span class="ruby-identifier">collect!</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span> <span class="ruby-identifier">y</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">y_max</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">range_upper</span> }
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">end</span>
    <span class="ruby-keyword kw">end</span>

    <span class="ruby-comment cmt"># print the results</span>
    <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@embedded</span>
      (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-identifier">opt_num</span>).<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;#{x}, #{y_values[x-1]}&quot;</span> }
    <span class="ruby-keyword kw">else</span>
      <span class="ruby-identifier">res</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>
      (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-identifier">opt_num</span>).<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">res</span> <span class="ruby-operator">&lt;&lt;</span> [ <span class="ruby-identifier">x</span> , <span class="ruby-identifier">y_values</span>[<span class="ruby-identifier">x</span><span class="ruby-operator">-</span><span class="ruby-value">1</span>] ] }
      <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">res</span>
    <span class="ruby-keyword kw">end</span>

  <span class="ruby-keyword kw">end</span></pre>
</body>
</html>